package com.lmc.gobang.game;

import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;
import org.springframework.web.socket.WebSocketSession;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
@Slf4j
@Component
public class OnlineUserManager {
    //表示当前用户在游戏大厅中的在线状态
    private ConcurrentHashMap<Integer, WebSocketSession> gameHall = new ConcurrentHashMap<>();
    //表示当前用户在游戏房间中的在线状态
    private ConcurrentHashMap<Integer, WebSocketSession> gameRoom = new ConcurrentHashMap<>();

   public  void enterGameHall(Integer userId , WebSocketSession session) {
       gameHall.put(userId, session);
   }

   public void exitGameHall(Integer userId) {
       log.info("exitGameHall");
       gameHall.remove(userId);
   }

   public WebSocketSession getFromGameHall(Integer userId) {
       log.info("用户 {} 已退出游戏大厅", userId);
       return gameHall.get(userId);
   }

   public void enterGameRoom(Integer userId , WebSocketSession session) {
       gameRoom.put(userId, session);
   }

   public void exitGameRoom(Integer userId) {
       log.info("用户 {} 已退出游戏房间", userId);
       gameRoom.remove(userId);
   }


   public WebSocketSession getFromGameRoom(Integer userId) {
       return gameRoom.get(userId);
   }

    // 遍历 gameRoom
    public void printGameRoom() {
       log.info("printGameRoom");
        gameRoom.forEach((userId, session) -> {
            System.out.print("User ID in Game Room: " + userId);
            // 可以在这里对 session 进行操作
            System.out.println("          session:"+session);
        });
    }
    // 遍历 gameHall
    public void printGameHall(){
       log.info("printGameHall");
        gameHall.forEach((userId, session) -> {
            System.out.print("User ID in Game Hall: " + userId);
            // 可以在这里对 session 进行操作
            System.out.println("          session:"+session);
        });
    }
}
